package com.woniuxy.propertyjava.controller;

import com.woniuxy.propertyjava.entity.PageBean;
import com.woniuxy.propertyjava.entity.ResponseResult;
import com.woniuxy.propertyjava.entity.Tenancyagreement;
import com.woniuxy.propertyjava.service.TenancyagreementService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Tenancyagreement)表控制层
 *
 * @author makejava
 * @since 2022-07-08 17:23:29
 */
@RestController
public class TenancyagreementController {
    /**
     * 服务对象
     */
    @Resource
    private TenancyagreementService tenancyagreementService;

    /**
     * 分页查询
     *
     *  tenancyagreement 筛选条件
     *  pageRequest      分页对象
     * @return 查询结果
     */
    @PostMapping("/tenancyagreement/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Tenancyagreement>> queryByPage(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody Tenancyagreement tenancyagreement) {
        PageBean<Tenancyagreement>pageBean= tenancyagreementService.queryByPage(tenancyagreement, pageIndex, pageSize);
        return new ResponseResult<>(200,"ok",pageBean);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/tenancyagreement/{id}")
    public ResponseEntity<Tenancyagreement> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tenancyagreementService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tenancyagreement 实体
     * @return 新增结果
     */
    @PostMapping("/tenancyagreement")
    public ResponseResult<Tenancyagreement> add(@RequestBody Tenancyagreement tenancyagreement) {
        tenancyagreementService.insert(tenancyagreement);
        return new ResponseResult<>(200, "ok", tenancyagreementService.queryById(tenancyagreement.getTacid()));
    }

    /**
     * 编辑数据
     *
     * @param tenancyagreement 实体
     * @return 编辑结果
     */
    @PutMapping("/tenancyagreement")
    public ResponseResult<Tenancyagreement> edit(@RequestBody Tenancyagreement tenancyagreement) {
        tenancyagreementService.update(tenancyagreement);
        return new ResponseResult<>(200, "ok", tenancyagreementService.queryById(tenancyagreement.getTacid()));
    }

    /**
     * 删除数据
     *
     *
     * @return 删除是否成功
     */
    @DeleteMapping("/tenancyagreement/{tacid}")
    public ResponseResult<Boolean> deleteById(@PathVariable("tacid") Integer tacid) {
        return new ResponseResult<>(200, "ok", tenancyagreementService.deleteById(tacid));
    }

}

